//
//  MXGraphicsView.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/10/8.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXGraphicsView.h"


#define LINE_WIDTH 2.0

#define SCR_BOUNDS_WIDTH self.bounds.size.width
#define SCR_BOUNDS_HEIGHT self.bounds.size.height

/**线偏移量*/
#define LINE_OFFSET SCR_BOUNDS_HEIGHT * 0.2

@interface MXGraphicsView (){
    CAShapeLayer *_layer;
}

@property (nonatomic, assign) PromptViewStyle style;

@end

@implementation MXGraphicsView

- (instancetype)initWithFrame:(CGRect)frame style:(PromptViewStyle)style {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        _style = style;
        [self drawRound];
    }
    return self;
}

- (void)drawRound {
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake( SCR_BOUNDS_WIDTH * 0.5, SCR_BOUNDS_HEIGHT * 0.5) radius:SCR_BOUNDS_WIDTH * 0.5 - LINE_WIDTH startAngle:0.0 endAngle:M_PI * 2 clockwise:NO];
    
    _layer = [CAShapeLayer layer];
    _layer.path = path.CGPath;
    _layer.fillColor = [UIColor clearColor].CGColor;
    _layer.strokeColor = [UIColor blackColor].CGColor;
    _layer.lineWidth = LINE_WIDTH;
    _layer.frame = self.bounds;
    [self.layer addSublayer:_layer];
    [self drawLineAnimation];
    
    switch (_style) {
        case PromptViewStyleSuccessful:
            [self drawTick];
            break;
        case PromptViewStyleFailure:
            [self drawCross];
        default:
            break;
    }
}

- (void)drawTick {
    CGFloat pathX = SCR_BOUNDS_WIDTH * 0.5 * (sqrt(2.0) - 1);
    CGFloat pathY = pathX / sqrt(2.0);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(pathX, pathY + LINE_OFFSET)];
    [path addLineToPoint:CGPointMake(SCR_BOUNDS_WIDTH * 0.5, SCR_BOUNDS_HEIGHT * 0.5 + LINE_OFFSET)];
    [path addLineToPoint:CGPointMake(SCR_BOUNDS_WIDTH  - pathX , pathY)];
    _layer = [CAShapeLayer layer];
    _layer.path = path.CGPath;
    _layer.fillColor = [UIColor clearColor].CGColor;
    _layer.strokeColor = [UIColor blackColor].CGColor;
    _layer.lineWidth = LINE_WIDTH;
    _layer.frame = self.bounds;
    [self.layer addSublayer:_layer];
    [self drawLineAnimation];
}

- (void)drawCross {
    CGFloat pathX = SCR_BOUNDS_WIDTH * 0.5 * (sqrt(2.0) - 1);
    CGFloat pathY = pathX / sqrt(2.0);
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(pathX, pathY)];
    [path addLineToPoint:CGPointMake(SCR_BOUNDS_WIDTH - pathX, SCR_BOUNDS_HEIGHT - pathY)];
    
    [path moveToPoint:CGPointMake(pathX, SCR_BOUNDS_HEIGHT - pathY)];
    [path addLineToPoint:CGPointMake(SCR_BOUNDS_WIDTH  - pathX , pathY)];
    _layer = [CAShapeLayer layer];
    _layer.path = path.CGPath;
    _layer.fillColor = [UIColor clearColor].CGColor;
    _layer.strokeColor = [UIColor blackColor].CGColor;
    _layer.lineWidth = LINE_WIDTH;
    _layer.frame = self.bounds;
    [self.layer addSublayer:_layer];
    [self drawLineAnimation];
    
}

- (void)drawLineAnimation{
    CABasicAnimation *bas = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    bas.duration = 0.5;
    bas.fromValue = [NSNumber numberWithInteger:0];
    bas.toValue = [NSNumber numberWithInteger:1];
    [_layer addAnimation:bas forKey:@"key"];
}
@end
